package com.uniqueyi.baselib.weightbase.edit;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.DigitsKeyListener;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.uniqueyi.baselib.R;
import com.uniqueyi.baselib.utilsbase.log.LogUtil;

/**
 * Created by
 * author:  cwy.
 * date:    2017/12/25.  13:44.
 * prj:     crxls.
 */
@SuppressLint("AppCompatCustomView")
public class IconEditText extends LinearLayout {

    /**
     * 总是显示右边删除图片(一直显示)
     */
    public static final int ALWAYS_SHOW = 0x0000;
    /**
     * 总是隐藏右边图片(不显示)
     */
    public static final int ALWAYS_HIND = 0x0001;
    /**
     * 有内容时显示(不管是否有焦点)
     */
    public static final int HAS_CONTENT_SHOW = 0x0002;
    /**
     * 有内容并且有焦点时显示
     */
    public static final int HAS_CONTENT_FOUCUS_SHOW = 0x0003;


    private Integer maxLength;
    /**
     * 右边删除按钮图片对象
     */
    private Drawable rightDrawableT;
    private Drawable leftDrawableT;
    private Drawable lineDrawableT;
    private Drawable rightDrawableF;
    private Drawable leftDrawableF;
    private Drawable lineDrawableF;
    private boolean rightFocus;

    private int etType;
    private int etColor;
    private int htColor;

    private int leftShowTime;
    private int rightShowTime;

    private String digits;
    private String hint;

    private EditText et;
    private ImageView leftIcon;
    private ImageView rightIcon;
    private LinearLayout ll_root;
    private ImageView iv_line;


    private ClickLinster clickLinster;

    public IconEditText(Context context) {
        this(context, null);
    }

    public IconEditText(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public IconEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(context).inflate(R.layout.view_icon_et, this);
        defIc();
        initAttr(attrs);
        init();
        initEvent();
    }

    private void defIc() {
//        rightDrawableT = getResources().getDrawable(R.mipmap.ic_wx);
//        leftDrawableT = getResources().getDrawable(R.mipmap.ic_wx);
        lineDrawableT = getResources().getDrawable(R.drawable.line_blue);
//        rightDrawableF = getResources().getDrawable(R.mipmap.ic_wx);
//        leftDrawableF = getResources().getDrawable(R.mipmap.ic_wx);
        lineDrawableF = getResources().getDrawable(R.drawable.line_gray);
    }

    private void init() {
        ll_root = (LinearLayout) findViewById(R.id.ll_root);
        iv_line = (ImageView) findViewById(R.id.iv_line);
        leftIcon = (ImageView) findViewById(R.id.iv_left);
        rightIcon = (ImageView) findViewById(R.id.iv_right);
        et = (EditText) findViewById(R.id.et);
        sets();
        changeIvStatu(leftIcon, et.getText().toString(), false, leftShowTime);
        changeIvStatu(rightIcon, et.getText().toString(), false, rightShowTime);
    }

    private void sets() {
        setLeftDrawableF(leftDrawableF);
        setEtType(etType);
        setRightDrawableF(rightDrawableF);
        setLineDrawableF(lineDrawableF);
        setLineDrawableT(lineDrawableT);
//        setDigits(digits);
        setHint(hint);
        setEtColor(etColor);
        setHtColor(htColor);

    }

    private void initEvent() {
        et.setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                changeFocus(b);
                changeIvStatu(leftIcon, et.getText().toString(), b, leftShowTime);
            }
        });
        et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                changeIvStatu(leftIcon, editable.toString(), isFocused(), leftShowTime);
                changeFocus(true);
            }
        });
        leftIcon.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                getFocus();
                if (clickLinster != null) {
                    clickLinster.leftIconClick();
                }
            }
        });
        rightIcon.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                getFocus();
                if (clickLinster != null) {
                    clickLinster.rightIconClick();
                }
            }
        });
    }

    private void changeFocus(boolean isFocus) {
        if (isFocus && !TextUtils.isEmpty(getText())) {
            iv_line.setImageDrawable(lineDrawableT);
            leftIcon.setImageDrawable(leftDrawableT);
            if (rightFocus) {
                rightIcon.setImageDrawable(rightDrawableT);
            }
        } else {
            if (TextUtils.isEmpty(getText())) {
                iv_line.setImageDrawable(lineDrawableF);
                leftIcon.setImageDrawable(leftDrawableF);
            }
            if (rightFocus) {
                rightIcon.setImageDrawable(rightDrawableF);
            }
        }
    }

    /**
     * 根据是否有内容和焦点以及设置的显示状态改变右边图片的显示状态
     *
     * @param iv
     * @param hasFocus
     */
    private void changeIvStatu(ImageView iv, String text, boolean hasFocus, int type) {
        if (ALWAYS_SHOW == type) { // 总是显示
            iv.setVisibility(VISIBLE);
        } else if (HAS_CONTENT_SHOW == type) { // 有内容就显示
            if (null != text && !TextUtils.isEmpty(text.toString())) {
                iv.setVisibility(VISIBLE);
            } else {
                iv.setVisibility(GONE);
            }
        } else if (HAS_CONTENT_FOUCUS_SHOW == type) { // 有内容并且有焦点时显示
            if (null != text && !TextUtils.isEmpty(text.toString()) && hasFocus) {
                iv.setVisibility(VISIBLE);
            } else {
                iv.setVisibility(GONE);
            }
        } else { // 总是隐藏
            iv.setVisibility(GONE);
        }
    }


    /**
     * 初始化属性
     *
     * @param attrs
     */
    private void initAttr(AttributeSet attrs) {
        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.IconEditText);
        maxLength = typedArray.getInteger(R.styleable.IconEditText_maxL, 20);
        rightDrawableT = typedArray.getDrawable(R.styleable.IconEditText_right_icon_t);
        leftDrawableT = typedArray.getDrawable(R.styleable.IconEditText_left_icon_t);
        lineDrawableT = typedArray.getDrawable(R.styleable.IconEditText_line_t);
        rightDrawableF = typedArray.getDrawable(R.styleable.IconEditText_right_icon_f);
        leftDrawableF = typedArray.getDrawable(R.styleable.IconEditText_left_icon_f);
        lineDrawableF = typedArray.getDrawable(R.styleable.IconEditText_line_f);
        rightFocus = typedArray.getBoolean(R.styleable.IconEditText_right_focus, false);
        etType = typedArray.getInt(R.styleable.IconEditText_input_type, InputType.TYPE_CLASS_TEXT);
        etColor = typedArray.getColor(R.styleable.IconEditText_et_color, getResources().getColor(android.R.color.black));
        htColor = typedArray.getColor(R.styleable.IconEditText_ht_color, etColor);
        leftShowTime = typedArray.getInt(R.styleable.IconEditText_left_icon_show_time, ALWAYS_SHOW);
        rightShowTime = typedArray.getInt(R.styleable.IconEditText_right_icon_show_time, ALWAYS_HIND);
        digits = typedArray.getString(R.styleable.IconEditText_digits);
        hint = typedArray.getString(R.styleable.IconEditText_hint);
        // 校验和设置自定义的属性
        typedArray.recycle();
    }

    public void setClickLinster(ClickLinster clickLinster) {
        this.clickLinster = clickLinster;
    }

    public void setMaxLength(Integer maxLength) {
        this.maxLength = maxLength;
        et.setMaxEms(maxLength);
    }

    public void setLeftDrawableT(Drawable leftDrawableT) {
        this.leftDrawableT = leftDrawableT;
        leftIcon.setImageDrawable(leftDrawableT);
    }

    public void setLeftDrawableF(Drawable leftDrawableF) {
        this.leftDrawableF = leftDrawableF;
        leftIcon.setImageDrawable(leftDrawableF);
    }

    public void setRightDrawableT(Drawable rightDrawableT) {
        this.rightDrawableT = rightDrawableT;
        rightIcon.setImageDrawable(rightDrawableT);
    }

    public void setRightDrawableF(Drawable rightDrawableF) {
        this.rightDrawableF = rightDrawableF;
        rightIcon.setImageDrawable(rightDrawableF);
    }

    public void setLineDrawableT(Drawable lineDrawableT) {
        this.lineDrawableT = lineDrawableT;
        iv_line.setImageDrawable(lineDrawableT);
    }

    public void setLineDrawableF(Drawable lineDrawableF) {
        this.lineDrawableF = lineDrawableF;
        iv_line.setImageDrawable(lineDrawableF);
    }

    public void setEtType(int etType) {
        this.etType = etType;
        et.setInputType(etType);
    }

    public void setEtColor(int etColor) {
        this.etColor = etColor;
        et.setTextColor(etColor);
    }

    public void setHtColor(int htColor) {
        this.htColor = htColor;
        LogUtil.getLogUtil().log("IconTxt", htColor);
        et.setHintTextColor(htColor);
    }

    public void setLeftShowTime(int leftShowTime) {
        this.leftShowTime = leftShowTime;
        changeIvStatu(leftIcon, et.getText().toString(), false, leftShowTime);
    }

    public void setRightShowTime(int rightShowTime) {
        this.rightShowTime = rightShowTime;
        changeIvStatu(rightIcon, et.getText().toString(), false, rightShowTime);
    }

    public void setDigits(String digits) {
        this.digits = digits;
        et.setKeyListener(DigitsKeyListener.getInstance(digits));
    }

    public void setHint(String hint) {
        this.hint = hint;
        et.setHint(hint);
    }

    public String getText() {
        return et.getText().toString().replace(" ", "").trim();
    }

    public String getRealText() {
        return et.getText().toString();
    }

    public void setText(String text) {
        et.setText(text);
    }

    public void getFocus() {
        et.requestFocus();
    }

    public void loseFocus() {
        et.clearFocus();
    }

    public EditText getEt() {
        return et;
    }

    public abstract static class ClickLinster {
        public void leftIconClick() {
        }

        public void rightIconClick() {
        }
    }
}
